<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>旧衣回收碳积分激励平台</title>
    <!-- 引入 Materialize CSS 国内源 -->
    <link rel="stylesheet" href="https://cdn.staticfile.org/materialize/1.0.0/css/materialize.min.css">
    <style>
        body {
            display: flex;
        }

        .sidebar {
            width: 200px;
            height: 100vh;
            background-color: #f8f9fa;
            padding-top: 20px;
        }

        .sidebar a {
            display: block;
            padding: 10px 20px;
            color: #333;
            text-decoration: none;
        }

        .sidebar a.active {
            background-color: #e9ecef;
        }

        .main-content {
            flex: 1;
            padding: 20px;
        }
    </style>
</head>

<body>
    <div class="sidebar">
        <a href="#" class="active" onclick="openTab('recyclingRecords')">回收记录</a>
        <a href="#" onclick="openTab('carbonPoints')">碳积分管理</a>
        <a href="#" onclick="openTab('users')">用户管理</a>
        <a href="#" onclick="openTab('rewards')">奖励兑换</a>
        <a href="#" onclick="openTab('statistics')">统计信息</a>
    </div>
    <div class="main-content">
        <!-- 回收记录 -->
        <div id="recyclingRecords" class="tab-content">
            <h2>回收记录</h2>
            <input type="text" id="recyclingSearch" placeholder="搜索回收记录" oninput="searchRecyclingRecords()">
            <button class="btn waves-effect waves-light" onclick="openAddRecyclingRecordModal()">添加回收记录</button>
            <table id="recyclingTable" class="striped">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>用户姓名</th>
                        <th>回收时间</th>
                        <th>回收重量</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody></tbody>
            </table>
        </div>
        <!-- 碳积分管理 -->
        <div id="carbonPoints" class="tab-content" style="display: none;">
            <h2>碳积分管理</h2>
            <input type="text" id="carbonPointsSearch" placeholder="搜索碳积分记录" oninput="searchCarbonPoints()">
            <button class="btn waves-effect waves-light" onclick="openAddCarbonPointsModal()">添加碳积分记录</button>
            <table id="carbonPointsTable" class="striped">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>用户姓名</th>
                        <th>积分数量</th>
                        <th>获取时间</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody></tbody>
            </table>
        </div>
        <!-- 用户管理 -->
        <div id="users" class="tab-content" style="display: none;">
            <h2>用户管理</h2>
            <input type="text" id="userSearch" placeholder="搜索用户" oninput="searchUsers()">
            <button class="btn waves-effect waves-light" onclick="openAddUserModal()">添加用户</button>
            <table id="userTable" class="striped">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>用户姓名</th>
                        <th>联系方式</th>
                        <th>注册时间</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody></tbody>
            </table>
        </div>
        <!-- 奖励兑换 -->
        <div id="rewards" class="tab-content" style="display: none;">
            <h2>奖励兑换</h2>
            <input type="text" id="rewardSearch" placeholder="搜索奖励记录" oninput="searchRewards()">
            <button class="btn waves-effect waves-light" onclick="openAddRewardModal()">添加奖励记录</button>
            <table id="rewardTable" class="striped">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>用户姓名</th>
                        <th>奖励名称</th>
                        <th>兑换时间</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody></tbody>
            </table>
        </div>
        <!-- 统计信息 -->
        <div id="statistics" class="tab-content" style="display: none;">
            <h2>统计信息</h2>
            <input type="text" id="statisticsSearch" placeholder="搜索统计信息" oninput="searchStatistics()">
            <button class="btn waves-effect waves-light" onclick="openAddStatisticsModal()">添加统计信息</button>
            <table id="statisticsTable" class="striped">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>统计项目</th>
                        <th>统计数值</th>
                        <th>统计时间</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody></tbody>
            </table>
        </div>
    </div>

    <!-- 回收记录添加/编辑模态框 -->
    <div id="recyclingRecordModal" class="modal">
        <div class="modal-content">
            <h4>添加/编辑回收记录</h4>
            <input type="hidden" id="recyclingRecordId">
            <label for="recyclingRecordUserName">用户姓名</label>
            <input type="text" id="recyclingRecordUserName">
            <label for="recyclingRecordTime">回收时间</label>
            <input type="datetime-local" id="recyclingRecordTime">
            <label for="recyclingRecordWeight">回收重量</label>
            <input type="number" id="recyclingRecordWeight">
        </div>
        <div class="modal-footer">
            <a href="#" class="modal-close waves-effect waves-green btn-flat">关闭</a>
            <a href="#" class="waves-effect waves-green btn-flat" onclick="saveRecyclingRecord()">保存</a>
        </div>
    </div>

    <!-- 碳积分管理添加/编辑模态框 -->
    <div id="carbonPointsModal" class="modal">
        <div class="modal-content">
            <h4>添加/编辑碳积分记录</h4>
            <input type="hidden" id="carbonPointsId">
            <label for="carbonPointsUserName">用户姓名</label>
            <input type="text" id="carbonPointsUserName">
            <label for="carbonPointsNumber">积分数量</label>
            <input type="number" id="carbonPointsNumber">
            <label for="carbonPointsTime">获取时间</label>
            <input type="datetime-local" id="carbonPointsTime">
        </div>
        <div class="modal-footer">
            <a href="#" class="modal-close waves-effect waves-green btn-flat">关闭</a>
            <a href="#" class="waves-effect waves-green btn-flat" onclick="saveCarbonPoints()">保存</a>
        </div>
    </div>

    <!-- 用户管理添加/编辑模态框 -->
    <div id="userModal" class="modal">
        <div class="modal-content">
            <h4>添加/编辑用户</h4>
            <input type="hidden" id="userId">
            <label for="userName">用户姓名</label>
            <input type="text" id="userName">
            <label for="userContact">联系方式</label>
            <input type="text" id="userContact">
            <label for="userRegistrationTime">注册时间</label>
            <input type="datetime-local" id="userRegistrationTime">
        </div>
        <div class="modal-footer">
            <a href="#" class="modal-close waves-effect waves-green btn-flat">关闭</a>
            <a href="#" class="waves-effect waves-green btn-flat" onclick="saveUser()">保存</a>
        </div>
    </div>

    <!-- 奖励兑换添加/编辑模态框 -->
    <div id="rewardModal" class="modal">
        <div class="modal-content">
            <h4>添加/编辑奖励记录</h4>
            <input type="hidden" id="rewardId">
            <label for="rewardUserName">用户姓名</label>
            <input type="text" id="rewardUserName">
            <label for="rewardName">奖励名称</label>
            <input type="text" id="rewardName">
            <label for="rewardExchangeTime">兑换时间</label>
            <input type="datetime-local" id="rewardExchangeTime">
        </div>
        <div class="modal-footer">
            <a href="#" class="modal-close waves-effect waves-green btn-flat">关闭</a>
            <a href="#" class="waves-effect waves-green btn-flat" onclick="saveReward()">保存</a>
        </div>
    </div>

    <!-- 统计信息添加/编辑模态框 -->
    <div id="statisticsModal" class="modal">
        <div class="modal-content">
            <h4>添加/编辑统计信息</h4>
            <input type="hidden" id="statisticsId">
            <label for="statisticsItem">统计项目</label>
            <input type="text" id="statisticsItem">
            <label for="statisticsValue">统计数值</label>
            <input type="number" id="statisticsValue">
            <label for="statisticsTime">统计时间</label>
            <input type="datetime-local" id="statisticsTime">
        </div>
        <div class="modal-footer">
            <a href="#" class="modal-close waves-effect waves-green btn-flat">关闭</a>
            <a href="#" class="waves-effect waves-green btn-flat" onclick="saveStatistics()">保存</a>
        </div>
    </div>

    <script src="https://cdn.staticfile.org/materialize/1.0.0/js/materialize.min.js"></script>
    <script>
        document.addEventListener('DOMContentLoaded', function () {
            var modals = document.querySelectorAll('.modal');
            M.Modal.init(modals);
        });

        // 初始化表格
        function init() {
            initRecyclingRecords();
            initCarbonPoints();
            initUsers();
            initRewards();
            initStatistics();
        }

        // 打开 tab 页面
        function openTab(tabName) {
            let tabContents = document.querySelectorAll('.tab-content');
            tabContents.forEach(content => {
                content.style.display = 'none';
            });
            let sidebarLinks = document.querySelectorAll('.sidebar a');
            sidebarLinks.forEach(link => {
                link.classList.remove('active');
            });
            document.getElementById(tabName).style.display = 'block';
            document.querySelector(`.sidebar a[onclick="openTab('${tabName}')"]`).classList.add('active');
        }

        // 回收记录管理
        function initRecyclingRecords() {
            let recyclingRecords = JSON.parse(localStorage.getItem('recyclingRecords')) || [
                { id: 1, userName: '用户1', time: '2024-01-01T09:00', weight: 10 },
                { id: 2, userName: '用户2', time: '2024-01-02T10:00', weight: 20 },
                { id: 3, userName: '用户3', time: '2024-01-03T11:00', weight: 30 },
                { id: 4, userName: '用户4', time: '2024-01-04T14:00', weight: 40 },
                { id: 5, userName: '用户5', time: '2024-01-05T15:00', weight: 50 },
                { id: 6, userName: '用户6', time: '2024-01-06T16:00', weight: 60 },
                { id: 7, userName: '用户7', time: '2024-01-07T17:00', weight: 70 },
                { id: 8, userName: '用户8', time: '2024-01-08T18:00', weight: 80 },
                { id: 9, userName: '用户9', time: '2024-01-09T19:00', weight: 90 },
                { id: 10, userName: '用户10', time: '2024-01-10T20:00', weight: 100 }
            ];
            localStorage.setItem('recyclingRecords', JSON.stringify(recyclingRecords));
            renderRecyclingRecords(recyclingRecords);
        }

        function renderRecyclingRecords(recyclingRecords) {
            let recyclingTable = document.getElementById('recyclingTable').getElementsByTagName('tbody')[0];
            recyclingTable.innerHTML = '';
            recyclingRecords.forEach(record => {
                let row = recyclingTable.insertRow();
                row.insertCell(0).textContent = record.id;
                row.insertCell(1).textContent = record.userName;
                row.insertCell(2).textContent = record.time;
                row.insertCell(3).textContent = record.weight;
                let actionsCell = row.insertCell(4);
                actionsCell.innerHTML = `<a class="btn waves-effect waves-light yellow" onclick="openEditRecyclingRecordModal(${record.id})">编辑</a>
                                         <a class="btn waves-effect waves-light red" onclick="deleteRecyclingRecord(${record.id})">删除</a>`;
            });
        }

        function openAddRecyclingRecordModal() {
            document.getElementById('recyclingRecordId').value = '';
            document.getElementById('recyclingRecordUserName').value = '';
            document.getElementById('recyclingRecordTime').value = '';
            document.getElementById('recyclingRecordWeight').value = '';
            let modal = M.Modal.getInstance(document.getElementById('recyclingRecordModal'));
            modal.open();
        }

        function openEditRecyclingRecordModal(id) {
            let recyclingRecords = JSON.parse(localStorage.getItem('recyclingRecords'));
            let record = recyclingRecords.find(r => r.id === id);
            document.getElementById('recyclingRecordId').value = record.id;
            document.getElementById('recyclingRecordUserName').value = record.userName;
            document.getElementById('recyclingRecordTime').value = record.time;
            document.getElementById('recyclingRecordWeight').value = record.weight;
            let modal = M.Modal.getInstance(document.getElementById('recyclingRecordModal'));
            modal.open();
        }

        function saveRecyclingRecord() {
            let recyclingRecords = JSON.parse(localStorage.getItem('recyclingRecords'));
            let id = document.getElementById('recyclingRecordId').value;
            let userName = document.getElementById('recyclingRecordUserName').value;
            let time = document.getElementById('recyclingRecordTime').value;
            let weight = document.getElementById('recyclingRecordWeight').value;
            if (id) {
                let index = recyclingRecords.findIndex(r => r.id === parseInt(id));
                recyclingRecords[index] = { id: parseInt(id), userName, time, weight };
            } else {
                let newId = recyclingRecords.length > 0 ? Math.max(...recyclingRecords.map(r => r.id)) + 1 : 1;
                recyclingRecords.push({ id: newId, userName, time, weight });
            }
            localStorage.setItem('recyclingRecords', JSON.stringify(recyclingRecords));
            renderRecyclingRecords(recyclingRecords);
            let modal = M.Modal.getInstance(document.getElementById('recyclingRecordModal'));
            modal.close();
        }

        function deleteRecyclingRecord(id) {
            let recyclingRecords = JSON.parse(localStorage.getItem('recyclingRecords'));
            recyclingRecords = recyclingRecords.filter(r => r.id!== id);
            localStorage.setItem('recyclingRecords', JSON.stringify(recyclingRecords));
            renderRecyclingRecords(recyclingRecords);
        }

        function searchRecyclingRecords() {
            let searchTerm = document.getElementById('recyclingSearch').value.toLowerCase();
            let recyclingRecords = JSON.parse(localStorage.getItem('recyclingRecords'));
            let filteredRecords = recyclingRecords.filter(record => record.userName.toLowerCase().includes(searchTerm));
            renderRecyclingRecords(filteredRecords);
        }

        // 碳积分管理
        function initCarbonPoints() {
            let carbonPoints = JSON.parse(localStorage.getItem('carbonPoints')) || [
                { id: 1, userName: '用户1', number: 100, time: '2024-01-01T09:00' },
                { id: 2, userName: '用户2', number: 200, time: '2024-01-02T10:00' },
                { id: 3, userName: '用户3', number: 300, time: '2024-01-03T11:00' },
                { id: 4, userName: '用户4', number: 400, time: '2024-01-04T14:00' },
                { id: 5, userName: '用户5', number: 500, time: '2024-01-05T15:00' },
                { id: 6, userName: '用户6', number: 600, time: '2024-01-06T16:00' },
                { id: 7, userName: '用户7', number: 700, time: '2024-01-07T17:00' },
                { id: 8, userName: '用户8', number: 800, time: '2024-01-08T18:00' },
                { id: 9, userName: '用户9', number: 900, time: '2024-01-09T19:00' },
                { id: 10, userName: '用户10', number: 1000, time: '2024-01-10T20:00' }
            ];
            localStorage.setItem('carbonPoints', JSON.stringify(carbonPoints));
            renderCarbonPoints(carbonPoints);
        }

        function renderCarbonPoints(carbonPoints) {
            let carbonPointsTable = document.getElementById('carbonPointsTable').getElementsByTagName('tbody')[0];
            carbonPointsTable.innerHTML = '';
            carbonPoints.forEach(point => {
                let row = carbonPointsTable.insertRow();
                row.insertCell(0).textContent = point.id;
                row.insertCell(1).textContent = point.userName;
                row.insertCell(2).textContent = point.number;
                row.insertCell(3).textContent = point.time;
                let actionsCell = row.insertCell(4);
                actionsCell.innerHTML = `<a class="btn waves-effect waves-light yellow" onclick="openEditCarbonPointsModal(${point.id})">编辑</a>
                                         <a class="btn waves-effect waves-light red" onclick="deleteCarbonPoints(${point.id})">删除</a>`;
            });
        }

        function openAddCarbonPointsModal() {
            document.getElementById('carbonPointsId').value = '';
            document.getElementById('carbonPointsUserName').value = '';
            document.getElementById('carbonPointsNumber').value = '';
            document.getElementById('carbonPointsTime').value = '';
            let modal = M.Modal.getInstance(document.getElementById('carbonPointsModal'));
            modal.open();
        }

        function openEditCarbonPointsModal(id) {
            let carbonPoints = JSON.parse(localStorage.getItem('carbonPoints'));
            let point = carbonPoints.find(p => p.id === id);
            document.getElementById('carbonPointsId').value = point.id;
            document.getElementById('carbonPointsUserName').value = point.userName;
            document.getElementById('carbonPointsNumber').value = point.number;
            document.getElementById('carbonPointsTime').value = point.time;
            let modal = M.Modal.getInstance(document.getElementById('carbonPointsModal'));
            modal.open();
        }

        function saveCarbonPoints() {
            let carbonPoints = JSON.parse(localStorage.getItem('carbonPoints'));
            let id = document.getElementById('carbonPointsId').value;
            let userName = document.getElementById('carbonPointsUserName').value;
            let number = document.getElementById('carbonPointsNumber').value;
            let time = document.getElementById('carbonPointsTime').value;
            if (id) {
                let index = carbonPoints.findIndex(p => p.id === parseInt(id));
                carbonPoints[index] = { id: parseInt(id), userName, number, time };
            } else {
                let newId = carbonPoints.length > 0 ? Math.max(...carbonPoints.map(p => p.id)) + 1 : 1;
                carbonPoints.push({ id: newId, userName, number, time });
            }
            localStorage.setItem('carbonPoints', JSON.stringify(carbonPoints));
            renderCarbonPoints(carbonPoints);
            let modal = M.Modal.getInstance(document.getElementById('carbonPointsModal'));
            modal.close();
        }

        function deleteCarbonPoints(id) {
            let carbonPoints = JSON.parse(localStorage.getItem('carbonPoints'));
            carbonPoints = carbonPoints.filter(p => p.id!== id);
            localStorage.setItem('carbonPoints', JSON.stringify(carbonPoints));
            renderCarbonPoints(carbonPoints);
        }

        function searchCarbonPoints() {
            let searchTerm = document.getElementById('carbonPointsSearch').value.toLowerCase();
            let carbonPoints = JSON.parse(localStorage.getItem('carbonPoints'));
            let filteredPoints = carbonPoints.filter(point => point.userName.toLowerCase().includes(searchTerm));
            renderCarbonPoints(filteredPoints);
        }

        // 用户管理
        function initUsers() {
            let users = JSON.parse(localStorage.getItem('users')) || [
                { id: 1, name: '用户1', contact: '13800138001', registrationTime: '2024-01-01T09:00' },
                { id: 2, name: '用户2', contact: '13800138002', registrationTime: '2024-01-02T10:00' },
                { id: 3, name: '用户3', contact: '13800138003', registrationTime: '2024-01-03T11:00' },
                { id: 4, name: '用户4', contact: '13800138004', registrationTime: '2024-01-04T14:00' },
                { id: 5, name: '用户5', contact: '13800138005', registrationTime: '2024-01-05T15:00' },
                { id: 6, name: '用户6', contact: '13800138006', registrationTime: '2024-01-06T16:00' },
                { id: 7, name: '用户7', contact: '13800138007', registrationTime: '2024-01-07T17:00' },
                { id: 8, name: '用户8', contact: '13800138008', registrationTime: '2024-01-08T18:00' },
                { id: 9, name: '用户9', contact: '13800138009', registrationTime: '2024-01-09T19:00' },
                { id: 10, name: '用户10', contact: '13800138010', registrationTime: '2024-01-10T20:00' }
            ];
            localStorage.setItem('users', JSON.stringify(users));
            renderUsers(users);
        }

        function renderUsers(users) {
            let userTable = document.getElementById('userTable').getElementsByTagName('tbody')[0];
            userTable.innerHTML = '';
            users.forEach(user => {
                let row = userTable.insertRow();
                row.insertCell(0).textContent = user.id;
                row.insertCell(1).textContent = user.name;
                row.insertCell(2).textContent = user.contact;
                row.insertCell(3).textContent = user.registrationTime;
                let actionsCell = row.insertCell(4);
                actionsCell.innerHTML = `<a class="btn waves-effect waves-light yellow" onclick="openEditUserModal(${user.id})">编辑</a>
                                         <a class="btn waves-effect waves-light red" onclick="deleteUser(${user.id})">删除</a>`;
            });
        }

        function openAddUserModal() {
            document.getElementById('userId').value = '';
            document.getElementById('userName').value = '';
            document.getElementById('userContact').value = '';
            document.getElementById('userRegistrationTime').value = '';
            let modal = M.Modal.getInstance(document.getElementById('userModal'));
            modal.open();
        }

        function openEditUserModal(id) {
            let users = JSON.parse(localStorage.getItem('users'));
            let user = users.find(u => u.id === id);
            document.getElementById('userId').value = user.id;
            document.getElementById('userName').value = user.name;
            document.getElementById('userContact').value = user.contact;
            document.getElementById('userRegistrationTime').value = user.registrationTime;
            let modal = M.Modal.getInstance(document.getElementById('userModal'));
            modal.open();
        }

        function saveUser() {
            let users = JSON.parse(localStorage.getItem('users'));
            let id = document.getElementById('userId').value;
            let name = document.getElementById('userName').value;
            let contact = document.getElementById('userContact').value;
            let registrationTime = document.getElementById('userRegistrationTime').value;
            if (id) {
                let index = users.findIndex(u => u.id === parseInt(id));
                users[index] = { id: parseInt(id), name, contact, registrationTime };
            } else {
                let newId = users.length > 0 ? Math.max(...users.map(u => u.id)) + 1 : 1;
                users.push({ id: newId, name, contact, registrationTime });
            }
            localStorage.setItem('users', JSON.stringify(users));
            renderUsers(users);
            let modal = M.Modal.getInstance(document.getElementById('userModal'));
            modal.close();
        }

        function deleteUser(id) {
            let users = JSON.parse(localStorage.getItem('users'));
            users = users.filter(u => u.id!== id);
            localStorage.setItem('users', JSON.stringify(users));
            renderUsers(users);
        }

        function searchUsers() {
            let searchTerm = document.getElementById('userSearch').value.toLowerCase();
            let users = JSON.parse(localStorage.getItem('users'));
            let filteredUsers = users.filter(user => user.name.toLowerCase().includes(searchTerm));
            renderUsers(filteredUsers);
        }

        // 奖励兑换
        function initRewards() {
            let rewards = JSON.parse(localStorage.getItem('rewards')) || [
                { id: 1, userName: '用户1', name: '奖励1', exchangeTime: '2024-01-01T09:00' },
                { id: 2, userName: '用户2', name: '奖励2', exchangeTime: '2024-01-02T10:00' },
                { id: 3, userName: '用户3', name: '奖励3', exchangeTime: '2024-01-03T11:00' },
                { id: 4, userName: '用户4', name: '奖励4', exchangeTime: '2024-01-04T14:00' },
                { id: 5, userName: '用户5', name: '奖励5', exchangeTime: '2024-01-05T15:00' },
                { id: 6, userName: '用户6', name: '奖励6', exchangeTime: '2024-01-06T16:00' },
                { id: 7, userName: '用户7', name: '奖励7', exchangeTime: '2024-01-07T17:00' },
                { id: 8, userName: '用户8', name: '奖励8', exchangeTime: '2024-01-08T18:00' },
                { id: 9, userName: '用户9', name: '奖励9', exchangeTime: '2024-01-09T19:00' },
                { id: 10, userName: '用户10', name: '奖励10', exchangeTime: '2024-01-10T20:00' }
            ];
            localStorage.setItem('rewards', JSON.stringify(rewards));
            renderRewards(rewards);
        }

        function renderRewards(rewards) {
            let rewardTable = document.getElementById('rewardTable').getElementsByTagName('tbody')[0];
            rewardTable.innerHTML = '';
            rewards.forEach(reward => {
                let row = rewardTable.insertRow();
                row.insertCell(0).textContent = reward.id;
                row.insertCell(1).textContent = reward.userName;
                row.insertCell(2).textContent = reward.name;
                row.insertCell(3).textContent = reward.exchangeTime;
                let actionsCell = row.insertCell(4);
                actionsCell.innerHTML = `<a class="btn waves-effect waves-light yellow" onclick="openEditRewardModal(${reward.id})">编辑</a>
                                         <a class="btn waves-effect waves-light red" onclick="deleteReward(${reward.id})">删除</a>`;
            });
        }

        function openAddRewardModal() {
            document.getElementById('rewardId').value = '';
            document.getElementById('rewardUserName').value = '';
            document.getElementById('rewardName').value = '';
            document.getElementById('rewardExchangeTime').value = '';
            let modal = M.Modal.getInstance(document.getElementById('rewardModal'));
            modal.open();
        }

        function openEditRewardModal(id) {
            let rewards = JSON.parse(localStorage.getItem('rewards'));
            let reward = rewards.find(r => r.id === id);
            document.getElementById('rewardId').value = reward.id;
            document.getElementById('rewardUserName').value = reward.userName;
            document.getElementById('rewardName').value = reward.name;
            document.getElementById('rewardExchangeTime').value = reward.exchangeTime;
            let modal = M.Modal.getInstance(document.getElementById('rewardModal'));
            modal.open();
        }

        function saveReward() {
            let rewards = JSON.parse(localStorage.getItem('rewards'));
            let id = document.getElementById('rewardId').value;
            let userName = document.getElementById('rewardUserName').value;
            let name = document.getElementById('rewardName').value;
            let exchangeTime = document.getElementById('rewardExchangeTime').value;
            if (id) {
                let index = rewards.findIndex(r => r.id === parseInt(id));
                rewards[index] = { id: parseInt(id), userName, name, exchangeTime };
            } else {
                let newId = rewards.length > 0 ? Math.max(...rewards.map(r => r.id)) + 1 : 1;
                rewards.push({ id: newId, userName, name, exchangeTime });
            }
            localStorage.setItem('rewards', JSON.stringify(rewards));
            renderRewards(rewards);
            let modal = M.Modal.getInstance(document.getElementById('rewardModal'));
            modal.close();
        }

        function deleteReward(id) {
            let rewards = JSON.parse(localStorage.getItem('rewards'));
            rewards = rewards.filter(r => r.id!== id);
            localStorage.setItem('rewards', JSON.stringify(rewards));
            renderRewards(rewards);
        }

        function searchRewards() {
            let searchTerm = document.getElementById('rewardSearch').value.toLowerCase();
            let rewards = JSON.parse(localStorage.getItem('rewards'));
            let filteredRewards = rewards.filter(reward => reward.userName.toLowerCase().includes(searchTerm));
            renderRewards(filteredRewards);
        }

        // 统计信息
        function initStatistics() {
            let statistics = JSON.parse(localStorage.getItem('statistics')) || [
                { id: 1, item: '统计项目1', value: 100, time: '2024-01-01T09:00' },
                { id: 2, item: '统计项目2', value: 200, time: '2024-01-02T10:00' },
                { id: 3, item: '统计项目3', value: 300, time: '2024-01-03T11:00' },
                { id: 4, item: '统计项目4', value: 400, time: '2024-01-04T14:00' },
                { id: 5, item: '统计项目5', value: 500, time: '2024-01-05T15:00' },
                { id: 6, item: '统计项目6', value: 600, time: '2024-01-06T16:00' },
                { id: 7, item: '统计项目7', value: 700, time: '2024-01-07T17:00' },
                { id: 8, item: '统计项目8', value: 800, time: '2024-01-08T18:00' },
                { id: 9, item: '统计项目9', value: 900, time: '2024-01-09T19:00' },
                { id: 10, item: '统计项目10', value: 1000, time: '2024-01-10T20:00' }
            ];
            localStorage.setItem('statistics', JSON.stringify(statistics));
            renderStatistics(statistics);
        }

        function renderStatistics(statistics) {
            let statisticsTable = document.getElementById('statisticsTable').getElementsByTagName('tbody')[0];
            statisticsTable.innerHTML = '';
            statistics.forEach(stat => {
                let row = statisticsTable.insertRow();
                row.insertCell(0).textContent = stat.id;
                row.insertCell(1).textContent = stat.item;
                row.insertCell(2).textContent = stat.value;
                row.insertCell(3).textContent = stat.time;
                let actionsCell = row.insertCell(4);
                actionsCell.innerHTML = `<a class="btn waves-effect waves-light yellow" onclick="openEditStatisticsModal(${stat.id})">编辑</a>
                                         <a class="btn waves-effect waves-light red" onclick="deleteStatistics(${stat.id})">删除</a>`;
            });
        }

        function openAddStatisticsModal() {
            document.getElementById('statisticsId').value = '';
            document.getElementById('statisticsItem').value = '';
            document.getElementById('statisticsValue').value = '';
            document.getElementById('statisticsTime').value = '';
            let modal = M.Modal.getInstance(document.getElementById('statisticsModal'));
            modal.open();
        }

        function openEditStatisticsModal(id) {
            let statistics = JSON.parse(localStorage.getItem('statistics'));
            let stat = statistics.find(s => s.id === id);
            document.getElementById('statisticsId').value = stat.id;
            document.getElementById('statisticsItem').value = stat.item;
            document.getElementById('statisticsValue').value = stat.value;
            document.getElementById('statisticsTime').value = stat.time;
            let modal = M.Modal.getInstance(document.getElementById('statisticsModal'));
            modal.open();
        }

        function saveStatistics() {
            let statistics = JSON.parse(localStorage.getItem('statistics'));
            let id = document.getElementById('statisticsId').value;
            let item = document.getElementById('statisticsItem').value;
            let value = document.getElementById('statisticsValue').value;
            let time = document.getElementById('statisticsTime').value;
            if (id) {
                let index = statistics.findIndex(s => s.id === parseInt(id));
                statistics[index] = { id: parseInt(id), item, value, time };
            } else {
                let newId = statistics.length > 0 ? Math.max(...statistics.map(s => s.id)) + 1 : 1;
                statistics.push({ id: newId, item, value, time });
            }
            localStorage.setItem('statistics', JSON.stringify(statistics));
            renderStatistics(statistics);
            let modal = M.Modal.getInstance(document.getElementById('statisticsModal'));
            modal.close();
        }

        function deleteStatistics(id) {
            let statistics = JSON.parse(localStorage.getItem('statistics'));
            statistics = statistics.filter(s => s.id!== id);
            localStorage.setItem('statistics', JSON.stringify(statistics));
            renderStatistics(statistics);
        }

        function searchStatistics() {
            let searchTerm = document.getElementById('statisticsSearch').value.toLowerCase();
            let statistics = JSON.parse(localStorage.getItem('statistics'));
            let filteredStatistics = statistics.filter(stat => stat.item.toLowerCase().includes(searchTerm));
            renderStatistics(filteredStatistics);
        }

        // 页面加载完成后初始化
        window.onload = function () {
            init();
        };
    </script>
</body>

</html>